package com.sbio.udiapp.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.sbio.udiapp.R;
import com.sbio.udiapp.di.ActivityManager;

import java.util.function.Consumer;

public class DialogUtil {

    public static void showCommonAlertDialog(String message) {
        Context context = ActivityManager.getCurrentActivity();
        //提示之前震动
        VibratorUtil.vibrator();
        LogUtils.i(message);
        new AlertDialog.Builder(context).setTitle("提示")
                .setMessage(message)
                .setPositiveButton("确定", (dialog, which) -> dialog.cancel())
                .show();
    }


    public static void showErrorAlertDialog(String message) {
        Context context = ActivityManager.getCurrentActivity();
        //提示之前震动
        VibratorUtil.vibrator();
        SoundPlayerUtil.playFail();
        new AlertDialog.Builder(context).setTitle("提示")
                .setMessage(message)
                .setPositiveButton("确定", (dialog, which) -> dialog.cancel())
                .show();
    }

    public static void showCommonAlertDialog(String message, Runnable after) {
        Context context = ActivityManager.getCurrentActivity();
        //提示之前震动
        VibratorUtil.vibrator();
        new AlertDialog.Builder(context).setTitle("提示")
                .setMessage(message)
                .setPositiveButton("确定", (dialog, which) -> {
                    dialog.cancel();
                    if (after != null) {
                        after.run();
                    }
                })
                .show();
    }

    public static void showCommonConfirmDialog(String message, Runnable after) {
        Context context = ActivityManager.getCurrentActivity();
        //提示之前震动
        VibratorUtil.vibrator();
        new AlertDialog.Builder(context).setTitle("提示")
                .setMessage(message)
                .setPositiveButton("确定", (dialog, which) -> {
                    dialog.cancel();
                    if (after != null) {
                        after.run();
                    }
                })
                .setNegativeButton("取消", (dialog, which) -> {
                    dialog.cancel();
                })
                .show();
    }

    /**
     * 显示一个带有EditText输入框的ConfirmDialog
     *
     * @param message
     * @param after
     */
    public static AlertDialog showConfirmDialogEditText(String message, Consumer<String> after) {
        Context context = ActivityManager.getCurrentActivity();
        // 创建一个 AlertDialog.Builder 对象
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(message);
        final EditText input = new EditText(context);
        builder.setView(input);
        input.requestFocus();
        builder.setPositiveButton("确定", (dialog, which) -> {
            String text = input.getText().toString();
            if (!StringUtils.isEmpty(text)) {
                after.accept(text);
            }
            // 处理输入的内容
        });
        builder.setNegativeButton("取消", (dialog, which) -> dialog.cancel());

        AlertDialog dialog = builder.create();
        input.setOnEditorActionListener((v1, actionId, event) -> {
            if (!StringUtils.isEmpty(v1.getText())) {
                dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
            }
            return false;
        });
        dialog.show();/**/
        return dialog;
    }



    /**
     * 显示一个带有EditText输入框的ConfirmDialog
     *
     * @param message
     * @param after
     */
    public static AlertDialog showConfirmDialogEditText(String message,String inputHint,String defaultInputValue, Consumer<String> after) {
        Context context = ActivityManager.getCurrentActivity();
        // 创建一个 AlertDialog.Builder 对象
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(message);
        final EditText input = new EditText(context);
        builder.setView(input);
        input.requestFocus();
        input.setHint(inputHint);
        input.setText(defaultInputValue);
        builder.setPositiveButton("确定", (dialog, which) -> {
            String text = input.getText().toString();
            if (!StringUtils.isEmpty(text)) {
                after.accept(text);
            }
            // 处理输入的内容
        });
        builder.setNegativeButton("取消", (dialog, which) -> dialog.cancel());

        AlertDialog dialog = builder.create();
        input.setOnEditorActionListener((v1, actionId, event) -> {
            if (!StringUtils.isEmpty(v1.getText())) {
                dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
            }
            return false;
        });
        dialog.show();/**/
        return dialog;
    }




}
